//
//  OMOrderRefundSubmitViewController.m
//  OMENGO_NEW
//
//  Created by 梁尚嘉 on 2017/5/16.
//  Copyright © 2017年 Kamfat. All rights reserved.
//

#import "OMOrderRefundSubmitViewController.h"
#import "OMOrderRefundInfoTableCell.h"
#import "OMOrderRefundReasonTableCell.h"
#import "OMInputTableCell.h"
#import "NSString+KTExt.h"
@interface OMOrderRefundSubmitViewController ()
@property (nonatomic, strong) NSArray <NSArray <BaseTableCell *>*>*cellSections;
@property (nonatomic, strong) NSArray <OMOrderRefundInfo*>*infos;
@property (nonatomic, strong) OMOrderRefundReasonTableCell *reasonCell;
@property (nonatomic, strong) OMInputTableCell *inputCell;
@property (nonatomic, strong) NSArray *reasons;
@end

@implementation OMOrderRefundSubmitViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupUI];
}

- (void)setupUI
{
    _infos = [OMOrderRefundInfo mj_objectArrayWithKeyValuesArray:
              @[
                @{@"title":@"及时处理",@"image":@"ic_order_refund_info1",@"desc":@"24小时内商家未处理，订单会自动取消。可先联系商家以便及时处理您的退单申请。"},
                @{@"title":@"全额退款",@"image":@"ic_order_refund_info2",@"desc":@"申请成功后，退款将于1-7个工作日退回到付款账户。"},
                ]];
    _reasonCell = [OMOrderRefundReasonTableCell new];
    _inputCell = [OMInputTableCell new];
    _inputCell.placehold = @"请尽量详细描述退款的原因";
    _inputCell.maxLength = 50;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    self.loading = true;
    self.title = @"申请退款";
    [self.confirmButton setTitle:@"提交申请" forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"联系商家" style:UIBarButtonItemStylePlain target:nil action:nil];
    @weakify(self);
    self.navigationItem.rightBarButtonItem.rac_command = [[RACCommand alloc]initWithSignalBlock:^RACSignal *(id input) {
        [@"400-930-2220" makeCall];
        return [RACSignal empty];
    }];

    [[RACSignal combineLatest:@[RACObserve(self.reasonCell, selectedReason),self.inputCell.textView.rac_textSignal] reduce:^id{
        @strongify(self);
        return @(self.reasonCell.selectedReason.length);
    }] subscribeNext:^(id x) {
        @strongify(self);
        self.confirmButton.enabled = [x boolValue];
    }];

    [self getRefundReason];

}

- (void)reloadDataFromNetWork
{
    [self getRefundReason];
}

- (void)getRefundReason
{
    [self startAnimating:YES];
    [KTHttpUtils GET:@"member/order/refundsReason.jhtml"
          completion:^(id responseObject, BOOL isSuccess, NSString *message) {
              if (isSuccess) {
                  [self startAnimating:NO];
                  self.reasons = responseObject[OMDatumKey];
              }else {
                  [self showNetWorkError];
              }
    }];
}

- (void)setReasons:(NSArray *)reasons
{
    _reasons = reasons;
    self.reasonCell.reasons = _reasons;
    [self.tableView reloadData];
}

#pragma mark - table view
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return _infos.count;
    }else if (section == 1) {
        return 1;
    }else if (section == 2) {
        return 1;
    }
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 1) {
        return SM_SIZE(60);
    }
    return DBL_EPSILON;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section != 1) {
        return nil;
    }
    UILabel *titleLabel = [UILabel new];
    titleLabel.font = SM_CFont(27);
    titleLabel.textColor = SM_blackColor;
    NSString *reasonInfo = @"  * 请选择1个退单原因";
    NSMutableAttributedString *att = [[NSMutableAttributedString alloc]initWithString:reasonInfo];
    [att setAttributes:@{NSForegroundColorAttributeName:SM_redColor} range:NSMakeRange(2, 1)];
    titleLabel.attributedText = att;
    return titleLabel;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        OMOrderRefundInfoTableCell *cell = [OMOrderRefundInfoTableCell tableViewCellWithTableView:tableView];
        [cell configureCellWithObj:_infos[indexPath.row]];
        return cell;
    }else if (indexPath.section == 1) {
        return _reasonCell;
    }else if (indexPath.section == 2) {
        return _inputCell;
    }
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return [OMOrderRefundInfoTableCell cellHeight];
    }else if (indexPath.section == 1) {
        return [OMOrderRefundReasonTableCell cellHeightWithObject:self.reasons];
    }else if (indexPath.section == 2) {
        return SM_SIZE(300);
    }
    return 0;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

}

- (void)submitAction:(UIButton *)sender
{
    [OMHUD HUDShieldLoading:@"正在提交申请"];
    [KTHttpUtils POST:@"member/order/refunds.jhtml"
           parameters:@{@"sn":self.order.sn,@"memo":self.inputCell.textView.text?:@"",@"content":self.reasonCell.selectedReason}
             progress:nil
           completion:^(id responseObject, BOOL isSuccess, NSString *message) {
        if (isSuccess) {
            SMNotificationPost(OMORDER_NEED_REFRESH_NOTIFICATION, nil);
            NaviPopAnimated(YES);
            [KTAlertView alertWithMessage:@"您的退款申请已提交成功！" Title:@"提交成功" ButtonTitles:nil withClickBlock:nil];
            [OMHUD HUDHide];
        }else {
            [OMHUD HUDInfo:message?:@"申请失败"];
        }
    }];
}
@end
